package top.suven.base.util.createcode;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import org.apache.commons.io.FileUtils;

import java.io.CharArrayWriter;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

public final class FreemakerUtil {

	private static Configuration cfg;
	///Users/joven/project/workspace/suven_server/suven-logic/
//	public static String basePath =  "D:/Java/workspace/Mytest/src/" ;//D:\Java\kugou7\sing_server\sing-core\src\main\java
	public static String projectPath = "/src/main/java/";
	public static String targerPath = "/src/main/java/";
	public static String targerControllerPath = "/src/main/java/";
	public static String tempProjectPath = "/base-utils/src/main/java/";
	public static String tempPack = "/templates/";
	public static String basePath = "";
	static{
		basePath = Class.class.getClass().getResource("/").getPath()
				.replace("/bin/", "")
				.replace("/build/classes/main/", "")
				.replace("out/production/", "")
				.replace("/production/", "/")
				.replace("/build/classes", "")
				.replace("/main/java", "")
				.replace("/java/main", "")
				.replace("/classes", "");
		basePath = basePath.replace("%20", " ");
		String xmlpath = new File(basePath).getParent();
		System.out.println(xmlpath);
		tempProjectPath = xmlpath + tempProjectPath;
		projectPath = basePath + projectPath;
		targerPath = basePath + targerPath;
	}
	public static synchronized void   init( ) throws IOException {
	 
	    String packageName = FreemakerUtil.class.getPackage().getName().replaceAll("\\.", "/");
		File baseFile = new File( tempProjectPath +  packageName +tempPack);
		cfg = new Configuration();
		cfg.setDefaultEncoding("utf-8");
		
		cfg.setDirectoryForTemplateLoading(baseFile);
		cfg.setObjectWrapper(new DefaultObjectWrapper());
	 
	}
	
	public static String getTemp(String templateFileName,Map<String, Object> dataMap) {
		String resultStr = "";
		try {
			Template temp = cfg.getTemplate(templateFileName + ".ftl","utf-8");
			Writer out = new CharArrayWriter();
			temp.process(dataMap, out);
			resultStr =  out.toString();
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultStr;
	}
	public static void
	writeTemp(Map<String, Object> dataMap,String template ,String outfile) {
		if(cfg==null){
			try {
				init( );
			} catch ( Exception e) {
				e.printStackTrace();
			}
		}
		
		String resultStr = "";
		try {
			Template temp = cfg.getTemplate(template  ,"utf-8");
			Writer out = new CharArrayWriter();
			temp.process(dataMap, out);
			resultStr =  out.toString();
			out.flush();
			out.close();
			
			File outFile = new File(outfile);
			if(!outFile.getParentFile().exists()){
				outFile.getParentFile().mkdirs();
			}
			if(outFile.exists()){
				System.err.println("java File is exists >>: " + outfile + "<<----------" );
				return ;
			}
			FileUtils.writeStringToFile(outFile, resultStr, "utf-8",true);
			
			System.out.println("write file: " + outFile.getName() + "; use " + template);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void writeTempDoc(Map<String, Object> dataMap,String template ,String outfile) {
		if(cfg==null){
			try {
				init( );
			} catch ( Exception e) {
				e.printStackTrace();
			}
		}
		
		String resultStr = "";
		try {
			Template temp = cfg.getTemplate(template  ,"utf-8");
			Writer out = new CharArrayWriter();
			temp.process(dataMap, out);
			resultStr =  out.toString();
			out.flush();
			out.close();
			
			File outFile = new File(outfile);
			if(!outFile.getParentFile().exists()){
				outFile.getParentFile().mkdirs();
			}
			if(outFile.exists()){
				System.err.println("java File is exists >>: " + outfile + "<<----------" );
//				return ;
			}
			FileUtils.writeStringToFile(outFile, resultStr, "utf-8",true);
			
			System.out.println("write file: " + outFile.getName() + "; use " + template);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//

//	public static void main(String[] args) throws Exception{
//		URL absPath=TestCreat.class.getResource("");
//	}

}
